;(function () {
	new Vue({
		el: '#incomeDetail',
		data: {
			isShowSettle: false,
			unitList: [],
			incomeData: {},
			revenue_recognition_history_list: [], 
			isShowSerrh: false,
			editFlag: false,
			contractData: {units: []},
			permissions: JSON.parse(window.localStorage.getItem('permissions')),
		},
		filters: {
			//千分位
			formatNum: function(n) {
			  return hlNumberUtil.formatNum(n)
			},
			timestamp: function(time) {
			  return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd HH:mm:ss')
			},
		},
		computed: {
			monthArr () {
				let arr = [];
				let number = 12;
				for (let i = 1; i <= number; i++) {
					arr.push(i)
				}
				return arr
			},
			acreage () {
				let number = 0;
				this.contractData.units.map(item => {
					number += Number(item.unitAcreage)
				})
				return number;
			}
		},
		mounted () {
			this.detailDt()
			this.contractDt()
			this.revenue_recognition_history()
		},
		methods: {
			validateAmout(i) {
				
				console.log(this.incomeData.amounts[i])
				if (!this.incomeData.amounts[i].withdrawAmount) {
					if (isNaN(this.incomeData.amounts[i].amount)) {
						this.incomeData.amounts[i].amount = ''
						hl.toast('数字填写不规范')
						return
					}
					if (Number(this.incomeData.amounts[i].amount) < 0) {
						this.incomeData.amounts[i].amount = ''
						hl.toast('金额不能小于0')
					}
				}
				if (this.incomeData.amounts[i].withdrawAmount) {
					if (isNaN(this.incomeData.amounts[i].withdrawAmount)) {
						this.incomeData.amounts[i].withdrawAmount = ''
						hl.toast('数字填写不规范')
						return
					}
					if (Number(this.incomeData.amounts[i].withdrawAmount) < 0) {
						this.incomeData.amounts[i].withdrawAmount = ''
						hl.toast('金额不能小于0')
					}
				}
			},
			
			editrr () {
				this.editFlag = true;
			},
			cancelrr () {
				this.editFlag = false;
				this.detailDt()
			},
			saverr () {
				let arr = [];
				this.incomeData.amounts.map(item => {
					if (!isNaN(item.amount)) {
						arr.push({id: item.id,amount: item.withdrawAmount ? item.withdrawAmount : item.amount})
					}
				})
				
				hlhttp.put({
					url: 'contract/revenue_recognition_amount/_check',
					data: {
						contract_id: hlUrlUtil.GetQueryString('id'),
						expenditure_id: hlUrlUtil.GetQueryString('expenditureId'),
						amounts: JSON.stringify(arr),
					},
					success: (data) => {
						if (data.changeFlag == 'Y') {
							hllayer.confirm1('保存后将影响相应财务数据与统计，是否确认变更？', () => {
							  hlhttp.put({
							  	url: 'contract/revenue_recognition_amount/_alter',
							  	data: {
							  		contract_id: hlUrlUtil.GetQueryString('id'),
										expenditure_id: hlUrlUtil.GetQueryString('expenditureId'),
							  		amounts: JSON.stringify(arr),
							  	},
							  	success: (data) => {
							  		this.detailDt()
							  		this.revenue_recognition_history()
							  		this.editFlag = false;
							  	}
							  })
							})
						}
						if (data.changeFlag == 'N') {
							hllayer.confirm1('未检测到金额变动，是否放弃变更', () => {
							  this.cancelrr()
							}, () => {
								this.cancelrr()
								$('.hllayer-mask,.hllayer-view').remove()
							})
						}
					}
				})
				
				
				
			},
			detailDt () {
				hlhttp.get({
					url: 'revenue_recognition',
					data: {
						contract_id: hlUrlUtil.GetQueryString('id'),
						expenditure_id: hlUrlUtil.GetQueryString('expenditureId')
					},
					success: (data) => {
						let monthArr = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
						let yearMonthArr = [];
						if (!data.year) return;
						data.year.map(item => {
							monthArr.map(item0 => {
								yearMonthArr.push({
									month: item + '-' + item0,
									amount: '--'
								})
							})
						})

						let arr = hlArrayUtil.diffent(data.amounts, yearMonthArr, 'month');
						data.amounts = arr.concat(data.amounts)
						data.amounts.map(item => {
							item.index = item.month.replace('-', '')
						})
						data.amounts = data.amounts.sort(hlArrayUtil.compareAsc('index'))
						this.incomeData = data
					}
				})
			},
			contractDt () {
				hlhttp.get({
					url: 'contract',
					data: {
						id: hlUrlUtil.GetQueryString('id')
					},
					success: (data) => {
						this.contractData = data
					}
				})
			},
			goToCustomerDetails(id){
				hlUrlUtil.goTo('customerDetails',{'id':id,'page':'customerManagement','ddtab':true},true)
			},
			goToContractDetails(id,proId,version){
				hlUrlUtil.goTo('contractNewDeatil',{'id':id,'proId':proId,'version':version,'ddtab':true},true)
			},
			revenue_recognition_history () {
				hlhttp.get({
					url: 'revenue_recognition_history',
					data: {
						contract_id: hlUrlUtil.GetQueryString('id'),
						expenditure_id: hlUrlUtil.GetQueryString('expenditureId')
					},
					success: (data) => {
						// let monthArr = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];						
						// if (!data.length) return;				
						
						
						// data.map(item => {
						// 	item.year.map(item => {
						// 		let arr = []
						// 		let yearMonthArr = [];
						// 		monthArr.map(item0 => {
						// 			yearMonthArr.push({
						// 				month: item + '-' + item0,
						// 				amount: '--'
						// 			})								  
						// 		})
						// 		arr = hlArrayUtil.diffent(item.amounts, yearMonthArr, 'month');
						// 		item.amounts = arr.concat(item.amounts)
						// 		item.amounts.map(item2 => {
						// 			item2.index = item2.month.replace('-', '')
						// 		})
						// 		item.amounts = item.amounts.sort(hlArrayUtil.compareAsc('index'))
						// 	})
						// })
				
						// let arr = hlArrayUtil.diffent(data.amounts, yearMonthArr, 'month');
						// data.amounts = arr.concat(data.amounts)
						// data.amounts.map(item => {
						// 	item.index = item.month.replace('-', '')
						// })
						// data.amounts = data.amounts.sort(hlArrayUtil.compareAsc('index'))
						this.revenue_recognition_history_list = data
					}
				})
			},
		}

	})
} ())
